﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TRACv2.Common.Enumerations;
using TRACv2.Domain.DTO;
using TRACv2.UI.Facade;

//Author: Morgan William Davis III
//Project: TRAC Ticket Manager v2
//Date: 14 Mar 2014

namespace TRACv2.UI
{
    public partial class FormAddTicket : Form
    {
        #region [ Fields ]

        private readonly TRACFacade _controller;
        private TicketDTO ticket;
        private Issue resolvedIssue;

        #endregion

        #region [ Constructor ]

        public FormAddTicket(TRACFacade controller)
        {
            InitializeComponent();
            _controller = controller;
            ticket = new TicketDTO();
            cmbPriority.DataSource = Enum.GetValues(typeof (Priority));
            cmbStatus.DataSource = Enum.GetValues(typeof (Status));
        }

        #endregion

        #region [ Control Events ]

        private void btnAddTicket_Click(object sender, EventArgs e)
        {
            var parsed = (Issue) Enum.Parse(typeof (Issue), txtIssue.Text, true);
            ticket.Issue = parsed;
            ticket.Priority = (Priority) cmbPriority.SelectedItem;
            ticket.Status = (Status) cmbStatus.SelectedItem;
            ticket.Note = txtNotes.Text;
            ticket.DateCreated = DateTime.Now;

            _controller.AddTicket(ticket);
            DialogResult = DialogResult.OK;
        }

        #endregion

        private void txtContact_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtContact.Text = form.ReturnEmployee;
                    ticket.Contact = form.EmployeeObject.ID;
                }
            }
        }

        private void txtCreator_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtCreator.Text = form.ReturnEmployee;
                    ticket.Creator = form.EmployeeObject.ID;
                }
            }
        }

        private void txtAssigned_Click(object sender, EventArgs e)
        {
            using (var form = new FormEmployeeList(_controller))
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtAssigned.Text = form.ReturnEmployee;
                    ticket.Assigned = form.EmployeeObject.ID;
                }
            }
        }

        private void txtIssue_Click(object sender, EventArgs e)
        {
            using (var form = new FormIssueSelect())
            {
                var result = form.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtIssue.Text = form.ReturnIssue;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        #region [ Methods ]

        

        #endregion
    }
}
